package v1

import (
	"bc/dao"
	"bc/service"
	"bc/util"
	"github.com/gin-gonic/gin"
	"strconv"
)

type bj struct {

}
func (t *bj)Create(c *gin.Context){

	data := make(map[string]interface{})
	if v,ok := c.Get("uid");ok{
		data["uid"] = v
	}
	if v,ok := c.GetPostForm("name");ok{
		data["name"] = v
	}
	if v,ok := c.GetPostForm("img_id");ok{
		img_id,err := strconv.Atoi(v)
		if err != nil {
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":err.Error(),
			})
			return
		}
		data["img_id"] = img_id
	}
	if v,ok := c.GetPostForm("ins_id");ok{
		ins_id,err := strconv.Atoi(v)
		if err != nil {
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":err.Error(),
			})
			return
		}
		data["ins_id"] = ins_id
	}
	if v,ok := c.GetPostForm("desc");ok{
		data["desc"] = v
	}
	if v,ok := c.GetPostForm("username");ok{
		data["username"] = v
	}
	if v,ok := c.GetPostForm("password");ok{
		data["password"] = v
	}
	if v,ok := c.GetPostForm("ip");ok{
		data["ip"] = v
	}
	if v,ok := c.GetPostForm("subnet");ok{
		data["subnet"] = v
	}
	if v,ok := data["gateway"];ok{
		data["gateway"] = v
	}
	if v,ok := data["tag"];ok{
		data["tag"] = v
	}

	//获取用户id
	data["uid"] = c.GetInt("uid")

	err := service.Bj.Create(data)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
	})

}
func (t *bj)Delete(c *gin.Context){

	bj_id_str := c.Param("bj_id")
	bj_id,err := strconv.Atoi(bj_id_str)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	err = service.Bj.Delete(map[string]interface{}{"bj_id":bj_id})
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
	})

}
func (t *bj)Update(c *gin.Context){

	bj_id_str := c.Param("bj_id")
	bj_id,err := strconv.Atoi(bj_id_str)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	where := make(map[string]interface{})
	where["bj_id"] = bj_id

	data := make(map[string]interface{})

	if v,ok := c.GetPostForm("name");ok{
		data["name"] = v
	}
	if v,ok := c.GetPostForm("img_id");ok{
		img_id,err := strconv.Atoi(v)
		if err != nil {
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":err.Error(),
			})
			return
		}
		data["img_id"] = img_id
	}
	if v,ok := c.GetPostForm("ins_id");ok{
		ins_id,err := strconv.Atoi(v)
		if err != nil {
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":err.Error(),
			})
			return
		}
		data["ins_id"] = ins_id
	}
	if v,ok := c.GetPostForm("desc");ok{
		data["desc"] = v
	}
	if v,ok := c.GetPostForm("username");ok{
		data["username"] = v
	}
	if v,ok := c.GetPostForm("password");ok{
		data["password"] = v
	}
	if v,ok := c.GetPostForm("ip");ok{
		data["ip"] = v
	}
	if v,ok := c.GetPostForm("subnet");ok{
		data["subnet"] = v
	}
	if v,ok := data["gateway"];ok{
		data["gateway"] = v
	}
	if v,ok := data["tag"];ok{
		data["tag"] = v
	}


	err = service.Bj.Update(where,data)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
	})
}
func (t *bj)Find(c *gin.Context){

	where := make(map[string]interface{})

	page,_ := strconv.Atoi(c.DefaultQuery("page","1"))

	pageSizeStr := c.DefaultQuery("pageSize",strconv.FormatInt(util.Config.App.PageSize,10))

	pageSize,_ := strconv.Atoi(pageSizeStr)

	searchSystem := c.DefaultQuery("search_system", "")

	searchValue := c.DefaultQuery("search_value", "")

	s := make(map[string]interface{})

	switch searchSystem {
	case "1":
		s["nickname"] = searchValue
	case "2":
		s["vm_name"]  = searchValue
	case "3":
		s["vir_type"] = searchValue
	case "4":
		s["platform"] = searchValue
	case "5":
		s["nickname"] = searchValue
		s["vm_name"]  = searchValue
		s["vir_type"] = searchValue
		s["platform"] = searchValue
	default:

	}

	bjs,total,err := service.Bj.Find(where,s,page,pageSize)

	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":bjs,
		"total":total,
	})

}

func (t *bj)First(c *gin.Context){

	var (
		bj_id int
		err error
		dao_bj *dao.Bj
	)

	bj_id,err = strconv.Atoi(c.Param("bj_id"))

	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	err, dao_bj = service.Bj.First(bj_id)

	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":dao_bj,
	})

}

var Bj = &bj{}
